﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类LanguageDAO。
    /// </summary>
    public class LanguageDAO
    {
        public LanguageDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int LanguageID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = LanguageID;

            int result = DbHelperSQL.RunProcedure("UP_Language_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(TimKen.DataEntity.Language model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@LanguageID", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,32),
					new SqlParameter("@Code", SqlDbType.Char,6),
					new SqlParameter("@Image", SqlDbType.VarChar,64),
					new SqlParameter("@Directory", SqlDbType.VarChar,32),
					new SqlParameter("@SortOrder", SqlDbType.Int,4)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Code;
            parameters[3].Value = model.Image;
            parameters[4].Value = model.Directory;
            parameters[5].Value = model.SortOrder;

            DbHelperSQL.RunProcedure("UP_Language_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.Language model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@LanguageID", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,32),
					new SqlParameter("@Code", SqlDbType.Char,6),
					new SqlParameter("@Image", SqlDbType.VarChar,64),
					new SqlParameter("@Directory", SqlDbType.VarChar,32),
					new SqlParameter("@SortOrder", SqlDbType.Int,4)};
            parameters[0].Value = model.LanguageID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Code;
            parameters[3].Value = model.Image;
            parameters[4].Value = model.Directory;
            parameters[5].Value = model.SortOrder;

            DbHelperSQL.RunProcedure("UP_Language_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int LanguageID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = LanguageID;

            DbHelperSQL.RunProcedure("UP_Language_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.Language GetModel(int LanguageID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = LanguageID;

            TimKen.DataEntity.Language model = new TimKen.DataEntity.Language();
            DataSet ds = DbHelperSQL.RunProcedure("UP_Language_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["LanguageID"].ToString() != "")
                {
                    model.LanguageID = int.Parse(ds.Tables[0].Rows[0]["LanguageID"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                model.Image = ds.Tables[0].Rows[0]["Image"].ToString();
                model.Directory = ds.Tables[0].Rows[0]["Directory"].ToString();
                if (ds.Tables[0].Rows[0]["SortOrder"].ToString() != "")
                {
                    model.SortOrder = int.Parse(ds.Tables[0].Rows[0]["SortOrder"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LanguageID,Name,Code,Image,Directory,SortOrder ");
            strSql.Append(" FROM Language ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
 
        #endregion  成员方法
    }
}
